package org.micode.demo.db.vo;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.micode.common.helper.I18nMsgHelper;

import java.lang.reflect.Field;

@Data
@Slf4j
@NoArgsConstructor
public class IdNameVO {

    private String id;
    private String name;

    public IdNameVO(Enum<?> e) {
        setId(getEnumId(e));
        setName(getEnumName(e));
    }

    public IdNameVO(String id, String name) {
        this.id = id;
        this.name = name;
    }

    private String getEnumId(Enum<?> e) {
        return getValueByFieldName("id", e);
    }

    private String getEnumName(Enum<?> e) {
        return getValueByFieldName("name", e);
    }

    private String getValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(object).toString();
        } catch (Exception e) {
            log.error(I18nMsgHelper.get("fail.IdNameVO-getValueByFieldName.call"), e);
            return "";
        }
    }

}
